package com.definesys.smecrailtransit.module.TotalTime.ViewController;

import android.support.annotation.NonNull;
import android.view.View;

import com.definesys.smecrailtransit.R;
import com.definesys.smecrailtransit.SmecManager.SmecRealm.RealmManager;
import com.definesys.smecrailtransit.adapter.SelctLineAdapter.SelectLineAdapter;
import com.definesys.smecrailtransit.adapter.SelectMonthAdapter.SelectMonthAdapter;
import com.definesys.smecrailtransit.adapter.SelectUserAdapter.SelectUserAdapter;
import com.definesys.smecrailtransit.bean.ApplyBean;
import com.definesys.smecrailtransit.bean.LineBean;
import com.definesys.smecrailtransit.bean.StationBean;
import com.definesys.smecrailtransit.databinding.FragmentTotalTimeBinding;
import com.definesys.smecrailtransit.module.TotalTime.TotalTimePresenter;
import com.definesys.smecrailtransit.module.TotalTime.bean.TotalTimeBean;
import com.definesys.smecrailtransit.utils.CommonUtils;
import com.prolificinteractive.materialcalendarview.CalendarDay;
import com.prolificinteractive.materialcalendarview.CalendarMode;
import com.prolificinteractive.materialcalendarview.MaterialCalendarView;
import com.prolificinteractive.materialcalendarview.OnRangeSelectedListener;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import static com.prolificinteractive.materialcalendarview.MaterialCalendarView.SELECTION_MODE_RANGE;


/**
 * Created by cjy on 2017/8/17.
 */

public class ViewController {
    private String selectUser;
    private String selectMonth;
    private String selectYear;
    private String selectLine;
    public String nowDate;
    private String startDate;
    private String endDate;
    public String eleContractNo;
    public int selectStation;
    private int calendarHeight;

    private ArrayList<StationBean> list;
    private ArrayList<ApplyBean> userList;
    private ArrayList<String> lineList;

    private FragmentTotalTimeBinding fragmentTotalTimeBinding;
    private TotalTimePresenter totalTimePresenter;
    protected RealmManager realmManager;

    private SelectMonthAdapter selectMonthAdapter;
    private SelectUserAdapter selectUserAdapter;
    private SelectLineAdapter selectLineAdapter;

    private MaterialCalendarView calendar;


    public ViewController(FragmentTotalTimeBinding fragmentTotalTimeBinding,TotalTimePresenter totalTimePresenter){
        this.fragmentTotalTimeBinding = fragmentTotalTimeBinding;
        this.totalTimePresenter = totalTimePresenter;
        this.realmManager=RealmManager.getInstance();

        this.list = new ArrayList<>();
        this.userList = new ArrayList<>();
        this.lineList = new ArrayList<>();

        this.selectLine = "12号线";
        this.selectUser = "全部运营商";

        ApplyBean apply1 = new ApplyBean(-1,"全部运营商");
        this.userList.add(apply1);
        ApplyBean apply2 = new ApplyBean(12299,"上海地铁第四运营有限公司");
        this.userList.add(apply2);
        ApplyBean apply3 = new ApplyBean(12323,"上海地铁第三运营有限公司");
        this.userList.add(apply3);
        ApplyBean apply4 = new ApplyBean(12356,"上海轨道交通十二号线发展有限公司");
        this.userList.add(apply4);

        //设置dialog背景层的点击事件
        fragmentTotalTimeBinding.backgroundChoice.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                fragmentTotalTimeBinding.choice.setVisibility(View.GONE);
            }
        });
        fragmentTotalTimeBinding.backgroundPull.setOnClickListener(v -> {
            fragmentTotalTimeBinding.pull.setVisibility(View.GONE);
            fragmentTotalTimeBinding.userImg.setBackgroundResource(R.drawable.month_form_bottom);

        });
        fragmentTotalTimeBinding.line.setOnClickListener(v -> {
            fragmentTotalTimeBinding.line.setVisibility(View.GONE);
            fragmentTotalTimeBinding.lineImg.setBackgroundResource(R.drawable.month_form_bottom);
        });

        //日历
        _setCalendar();

        //设置月份下拉列表点击事件
        fragmentTotalTimeBinding.choiceMonth.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(fragmentTotalTimeBinding.choice.getVisibility() == View.GONE){
                    fragmentTotalTimeBinding.choice.setVisibility(View.VISIBLE);
                }else{
                    fragmentTotalTimeBinding.choice.setVisibility(View.GONE);
                }
                fragmentTotalTimeBinding.line.setVisibility(View.GONE);
                fragmentTotalTimeBinding.lineImg.setBackgroundResource(R.drawable.month_form_bottom);
                fragmentTotalTimeBinding.pull.setVisibility(View.GONE);
                fragmentTotalTimeBinding.userImg.setBackgroundResource(R.drawable.month_form_bottom);
            }
        });

        //设置线路下拉列表点击事件
        fragmentTotalTimeBinding.choiceLine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectLineAdapter.notifyDataSetChanged();
                if(fragmentTotalTimeBinding.line.getVisibility() == View.GONE){
                    fragmentTotalTimeBinding.line.setVisibility(View.VISIBLE);
                    fragmentTotalTimeBinding.lineImg.setBackgroundResource(R.drawable.month_form_top);
                }else{
                    fragmentTotalTimeBinding.line.setVisibility(View.GONE);
                    fragmentTotalTimeBinding.lineImg.setBackgroundResource(R.drawable.month_form_bottom);
                }
                fragmentTotalTimeBinding.choice.setVisibility(View.GONE);
                fragmentTotalTimeBinding.pull.setVisibility(View.GONE);
                fragmentTotalTimeBinding.userImg.setBackgroundResource(R.drawable.month_form_bottom);
            }
        });

        //设置运营商下拉列表点击事件
        fragmentTotalTimeBinding.choiceUser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectUserAdapter.select_user = selectUser;
                selectUserAdapter.notifyDataSetChanged();
                fragmentTotalTimeBinding.invalidateAll();

                if(fragmentTotalTimeBinding.pull.getVisibility() == View.GONE){
                    fragmentTotalTimeBinding.pull.setVisibility(View.VISIBLE);
                    fragmentTotalTimeBinding.userImg.setBackgroundResource(R.drawable.month_form_top);
                }else{
                    fragmentTotalTimeBinding.pull.setVisibility(View.GONE);
                    fragmentTotalTimeBinding.userImg.setBackgroundResource(R.drawable.month_form_bottom);
                }
                fragmentTotalTimeBinding.line.setVisibility(View.GONE);
                fragmentTotalTimeBinding.lineImg.setBackgroundResource(R.drawable.month_form_bottom);
                fragmentTotalTimeBinding.choice.setVisibility(View.GONE);
            }
        });

    }

    
    //获取数据
    public void getNetWorkData(){
        //设置参数
        HashMap<String, Object> param = new HashMap<>();
        param.put("from",startDate);
        param.put("to",endDate);
        int l=this.getSelectLine()!=null ? this.getSelectLine().length()-2:0;
        param.put("line",this.getSelectLine().substring(0,l));
        //网络请求
        this.totalTimePresenter.getLineTime(param);

    }

    //获取本地数据
    public void getLocalData(TotalTimeBean data){
//        //获取运营商数据
//        this.userList = data.getApply();
//        ApplyBean allApply = new ApplyBean(-1,"全部运营商");
//        this.userList.add(0,allApply);

        //获取站点信息
        this.list = data.getStation();
        ArrayList<StationBean> arrayList = new ArrayList<>();
        if(!this.selectUser.equals("全部运营商")){
            for(int i=0 ; i< this.list.size();i++){
                if(this.list.get(i).getApplyId() == this.getNowApplyId()){
                    arrayList.add(this.list.get(i));
                }
            }
            this.list = arrayList;
        }

        //排序处理
        ArrayList<StationBean> dataList = this.list;
        ArrayList<StationBean> resultList = new ArrayList<>();
        int length = this.list.size();
        for(int i = 0; i < length ;i++){

            int min = dataList.get(0).getSeq();
            int index =0;
            for(int j = 0; j < dataList.size() ;j++){
                if(dataList.get(j).getSeq() < min){
                    min = dataList.get(j).getSeq();
                    index = j;
                }
            }

            if(i != 0){
                if(resultList.get(i-1).getSeq() != dataList.get(index).getSeq()-1){
                    StationBean s = new StationBean();
                    s.setSeq(-1);
                    resultList.add(s);
                }
            }
            resultList.add(dataList.get(index));


            dataList.remove(index);
        }

        this.list = resultList;

        fragmentTotalTimeBinding.invalidateAll();

    }

    private int getNowLineId(ArrayList<LineBean> lineList){

        if(CommonUtils.notEmpty(lineList)){
            for(int i=0 ;i<lineList.size();i++){
                if(lineList.get(i).getName().equals(this.selectLine)){
                    return lineList.get(i).getLineId();
                }
            }
            return lineList.get(0).getLineId();
        }
        return -1;
    }

    private int getNowApplyId(){
        for(int i=0 ;i<this.userList.size();i++){
            if(this.userList.get(i).getName().equals(this.selectUser)){
                return  this.userList.get(i).getApplyId();
            }
        }
        return this.userList.get(0).getApplyId();
    }

    //初始化日历插件
    private void _setCalendar(){
        //保存控件高度
        calendarHeight = fragmentTotalTimeBinding.calendar.getHeight();

        //获取系统时间
        SimpleDateFormat sDateFormat =  new SimpleDateFormat("yyyy-MM-dd");
        Calendar c = Calendar.getInstance();
        //过去七天
        c.setTime(new Date());
        c.add(Calendar.DATE, - 7);
        Date d = c.getTime();
        this.nowDate = sDateFormat.format(new Date());
        this.startDate = sDateFormat.format(d);
        this.endDate = this.nowDate;

        calendar = fragmentTotalTimeBinding.calendar;
        calendar.state().edit()
                .setFirstDayOfWeek(Calendar.MONDAY)
                .setMaximumDate(CalendarDay.from(Calendar.getInstance().get(Calendar.YEAR)
                        , Calendar.getInstance().get(Calendar.MONTH)
                        , Calendar.getInstance().get(Calendar.DATE)))
                .setCalendarDisplayMode(CalendarMode.MONTHS)
                .commit();
        calendar.setSelectionMode(SELECTION_MODE_RANGE);
        calendar.setSelectionColor(fragmentTotalTimeBinding.getRoot().getContext().getResources().getColor(R.color.calendar_select));
        calendar.setArrowColor(fragmentTotalTimeBinding.getRoot().getContext().getResources().getColor(R.color.monthForm_line));
        calendar.setOnRangeSelectedListener(new OnRangeSelectedListener() {
            @Override
            public void onRangeSelected(@NonNull MaterialCalendarView widget, @NonNull List<CalendarDay> dates) {
                startDate = sDateFormat.format(dates.get(0).getDate());
                endDate = sDateFormat.format(dates.get(dates.size()-1).getDate());
                fragmentTotalTimeBinding.startDate.setText(startDate);
                fragmentTotalTimeBinding.endDate.setText(endDate);
                getNetWorkData();
                //收起动画
//                fragmentTotalTimeBinding.getRoot().postDelayed(new Runnable() {
//                    @Override
//                    public void run() {
//                        //fragmentTotalTimeBinding.choice.setVisibility(View.GONE);
//                        //属性动画对象
//                        ValueAnimator va ;
//                        //显示view，高度从height变到0值
//                        va = ValueAnimator.ofInt(calendarHeight,0);
//                        va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
//                            @Override
//                            public void onAnimationUpdate(ValueAnimator valueAnimator) {
//                                //获取当前的height值
//                                int h =(Integer)valueAnimator.getAnimatedValue();
//                                //动态更新view的高度
//                                fragmentTotalTimeBinding.choice.getLayoutParams().height = h;
//                                fragmentTotalTimeBinding.choice.requestLayout();
//                            }
//                        });
//                        va.setDuration(800);
//                        //开始动画
//                        va.start();
//                    }
//                },300);
                fragmentTotalTimeBinding.choice.setVisibility(View.GONE);
            }
        });

    }


    //set、get方法
    public ArrayList<StationBean> getList() {
        return list;
    }

    public void setList(ArrayList<StationBean> list) {
        this.list = list;
    }

    public ArrayList<ApplyBean> getUserList() {
        return userList;
    }

    public void setUserList(ArrayList<ApplyBean> userList) {
        this.userList = userList;
    }

    public String getSelectUser() {
        return selectUser;
    }

    public void setSelectUser(String selectUser) {
        this.selectUser = selectUser;
    }

    public String getSelectLine() {
        return selectLine;
    }

    public void setSelectLine(String selectLine) {
        this.selectLine = selectLine;
    }

    public ArrayList<String> getLineList() {
        return lineList;
    }

    public void setLineList(ArrayList<String> lineList) {
        this.lineList = lineList;
    }

    public FragmentTotalTimeBinding getfragmentTotalTimeBinding() {
        return fragmentTotalTimeBinding;
    }

    public void setfragmentTotalTimeBinding(FragmentTotalTimeBinding fragmentTotalTimeBinding) {
        this.fragmentTotalTimeBinding = fragmentTotalTimeBinding;
    }

    public String getSelectYear() {
        return selectYear;
    }

    public void setSelectYear(String selectYear) {
        this.selectYear = selectYear;
    }

    public String getSelectMonth() {
        return selectMonth;
    }

    public void setSelectMonth(String selectMonth) {
        this.selectMonth = selectMonth;
    }

    public SelectMonthAdapter getSelectMonthAdapter() {
        return selectMonthAdapter;
    }

    public void setSelectMonthAdapter(SelectMonthAdapter selectMonthAdapter) {
        this.selectMonthAdapter = selectMonthAdapter;
    }

    public SelectUserAdapter getSelectUserAdapter() {
        return selectUserAdapter;
    }

    public void setSelectUserAdapter(SelectUserAdapter selectUserAdapter) {
        this.selectUserAdapter = selectUserAdapter;
    }

    public SelectLineAdapter getSelectLineAdapter() {
        return selectLineAdapter;
    }

    public void setSelectLineAdapter(SelectLineAdapter selectLineAdapter) {
        this.selectLineAdapter = selectLineAdapter;
    }


    public String getStartDate() {
        return startDate;
    }

    public void setStartDate(String startDate) {
        this.startDate = startDate;
    }

    public String getEndDate() {
        return endDate;
    }

    public void setEndDate(String endDate) {
        this.endDate = endDate;
    }
}

